home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / DC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  12.4 KB  |  582 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. // Implementation of class TDC
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_DC_H)
  11. # include <owl/dc.h>
  12. #endif
  13.  
  14. OWL_DIAGINFO;
  15. DIAG_DECLARE_GROUP(OwlGDI);        // General GDI diagnostic group
  16.  
  17. //
  18. //
  19. //
  20. void
  21. TDC::Init()
  22. {
  23.   OrgBrush = 0;
  24.   OrgPen = 0;
  25.   OrgFont = 0;
  26.   OrgPalette = 0;
  27. #if defined(BI_PLAT_WIN32)
  28.   OrgTextBrush = 0;
  29. #endif
  30. }
  31.  
  32. //
  33. //
  34. //
  35. TDC::TDC(HDC handle)
  36. :
  37.   TGdiBase(handle, NoAutoDelete)
  38. {
  39.   Init();
  40.   TRACEX(OwlGDI, OWL_CDLEVEL, "TDC constructed @" << (void*)this <<
  41.     " from handle" << uint(handle));
  42. }
  43.  
  44. //
  45. // Following two ctors are for use by derived classes only
  46. //
  47. TDC::TDC()
  48. {
  49.   Init();
  50.   TRACEX(OwlGDI, OWL_CDLEVEL, "TDC constructed @" << (void*)this);
  51. }
  52.  
  53. //
  54. //
  55. //
  56. TDC::TDC(HDC handle, TAutoDelete autoDelete)
  57. :
  58.   TGdiBase(handle, autoDelete)
  59. {
  60.   Init();
  61.   TRACEX(OwlGDI, OWL_CDLEVEL, "TDC constructed @" << (void*)this <<
  62.     " from handle" << uint(handle));
  63. }
  64.  
  65. //
  66. // Default dtor does not delete Handle
  67. //
  68. TDC::~TDC()
  69. {
  70.   RestoreObjects();
  71.   TRACEX(OwlGDI, OWL_CDLEVEL, "TDC destructed @" << (void*)this);
  72. }
  73.  
  74. //
  75. //
  76. //
  77. HDC
  78. TDC::GetAttributeHDC() const
  79. {
  80.   return HDC(Handle);
  81. }
  82.  
  83. //
  84. //
  85. //
  86. void
  87. TDC::SelectObject(const TPen& pen)
  88. {
  89.   TRACEX(OwlGDI, 1, "TDC::SelectPen @" << (void*)this <<
  90.     " pen @" << (void*)&pen);
  91.   HPEN oldPen = (HPEN)::SelectObject(GetHDC(), pen);
  92.   if (oldPen) {
  93.     TGdiObject::RefInc(pen);
  94.     if (uint(oldPen) > 1)
  95.       if (!OrgPen)
  96.         OrgPen = oldPen;
  97.       else
  98.         TGdiObject::RefDec(oldPen, false);
  99.   }
  100. }
  101.  
  102. //
  103. //
  104. //
  105. void
  106. TDC::SelectObject(const TBrush& brush)
  107. {
  108.   TRACEX(OwlGDI, 1, "TDC::SelectBrush @" << (void*)this <<
  109.     " brush @" << (void*)&brush);
  110.   HBRUSH oldBrush = (HBRUSH)::SelectObject(GetHDC(), brush);
  111.   if (oldBrush) {
  112.     TGdiObject::RefInc(brush);
  113.     if (uint(oldBrush) > 1)
  114.       if (!OrgBrush)
  115.         OrgBrush = oldBrush;
  116.       else
  117.         TGdiObject::RefDec(oldBrush, false);
  118.   }
  119. }
  120.  
  121. //
  122. //
  123. //
  124. void
  125. TDC::SelectObject(const TFont& font)
  126. {
  127.   TRACEX(OwlGDI, 1, "TDC::SelectFont @" << (void*)this <<
  128.     " font @" << (void*)&font);
  129.   HFONT oldFont = (HFONT)::SelectObject(GetHDC(), font);
  130.   if (oldFont) {
  131.     TGdiObject::RefInc(font);
  132.     if (uint(oldFont) > 1)
  133.       if (!OrgFont)
  134.         OrgFont = oldFont;
  135.       else
  136.         TGdiObject::RefDec(oldFont, false);
  137.   }
  138. }
  139.  
  140. //
  141. //
  142. //
  143. void
  144. TDC::SelectObject(const TPalette& palette, bool forceBackground)
  145. {
  146.   TRACEX(OwlGDI, 1, "TDC::SelectPalette @" << (void*)this <<
  147.     " palette @" << (void*)&palette);
  148.   HPALETTE oldPalette = ::SelectPalette(GetHDC(), palette, forceBackground);
  149.   if (oldPalette) {
  150.     TGdiObject::RefInc(palette);
  151.     if (uint(oldPalette) > 1)
  152.       if (!OrgPalette)
  153.         OrgPalette = oldPalette;
  154.       else
  155.         TGdiObject::RefDec(oldPalette, false);
  156.   }
  157. }
  158.  
  159. //
  160. //
  161. //
  162. void
  163. TDC::SelectStockObject(int index)
  164. {
  165.   PRECONDITION(::GetStockObject(index));
  166.   TRACEX(OwlGDI, 1, "TDC::SelectStockObject @" << (void*)this <<
  167.     " index " << index);
  168.   HANDLE oldObj = ::SelectObject(GetHDC(), ::GetStockObject(index));
  169.   if (uint(oldObj) > 1)
  170.     TGdiObject::RefDec(oldObj, false);
  171. }
  172.  
  173. //
  174. //
  175. //
  176. void
  177. TDC::RestorePen()
  178. {
  179.   TRACEX(OwlGDI, 1, "TDC::RestorePen @" << (void*)this);
  180.   if (OrgPen) {
  181.     TGdiObject::RefDec(::SelectObject(GetHDC(), OrgPen), false);
  182.     OrgPen = 0;
  183.   }
  184. }
  185.  
  186. //
  187. //
  188. //
  189. void
  190. TDC::RestoreBrush()
  191. {
  192.   TRACEX(OwlGDI, 1, "TDC::RestoreBrush @" << (void*)this);
  193.   if (OrgBrush) {
  194.     TGdiObject::RefDec(::SelectObject(GetHDC(), OrgBrush), false);
  195.     OrgBrush = 0;
  196.   }
  197. }
  198.  
  199. //
  200. //
  201. //
  202. void
  203. TDC::RestoreFont()
  204. {
  205.   TRACEX(OwlGDI, 1, "TDC::RestoreFont @" << (void*)this);
  206.   if (OrgFont) {
  207.     TGdiObject::RefDec(::SelectObject(GetHDC(), OrgFont), false);
  208.     OrgFont = 0;
  209.   }
  210. }
  211.  
  212. //
  213. //
  214. //
  215. void
  216. TDC::RestorePalette()
  217. {
  218.   TRACEX(OwlGDI, 1, "TDC::RestorePalette @" << (void*)this);
  219.   if (OrgPalette) {
  220.     TGdiObject::RefDec(::SelectPalette(GetHDC(), OrgPalette, false), false);
  221.     OrgPalette = 0;
  222.   }
  223. }
  224.  
  225. #if defined(BI_PLAT_WIN32)
  226. //
  227. //
  228. //
  229. void
  230. TDC::RestoreTextBrush()
  231. {
  232.   TRACEX(OwlGDI, 1, "TDC::RestoreTextBrush @" << (void*)this);
  233.   if (OrgTextBrush) {
  234.     TGdiObject::RefDec(::SelectObject(GetHDC(), OrgTextBrush), false);
  235.     OrgTextBrush = 0;
  236.   }
  237. }
  238. #endif
  239.  
  240. //
  241. //
  242. //
  243. void
  244. TDC::RestoreObjects()
  245. {
  246.   if (Handle) {
  247.     RestorePen();
  248.     RestoreBrush();
  249.     RestoreFont();
  250.     RestorePalette();
  251. #if defined(BI_PLAT_WIN32)
  252.     RestoreTextBrush();
  253. #endif
  254.   }
  255. }
  256.  
  257. //
  258. // Subset of Win32 GetCurrentObject for Win16 (& Win32s!), or just a straight
  259. // call for normal win32
  260. //
  261. HANDLE
  262. TDC::GetCurrentObject(uint objectType) const
  263. {
  264. #if defined(BI_PLAT_WIN32) && !defined(WIN32S_SUPPORT)
  265.     return ::GetCurrentObject(GetHDC(), objectType);
  266. #else
  267.  
  268. # if defined(BI_PLAT_WIN32)
  269.   static bool notImplemented = false;
  270.  
  271.   // Try the Win32 GetCurrentObject() function, will fail under Win32s
  272.   //
  273.   if (!notImplemented) {
  274.     HGDIOBJ curObj = ::GetCurrentObject(GetHDC(), objectType);
  275.     if (::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
  276.       return curObj;
  277.     notImplemented = true;
  278.   }
  279. # endif
  280.  
  281.   // Get the currently selected tool object of a given type by shoving a stock
  282.   // object into it slot and catching it when it pops out. Then restore it.
  283.   //
  284.   HGDIOBJ curObj;
  285.   switch (objectType) {
  286.     case OBJ_PEN:
  287.       curObj = ::SelectObject(GetHDC(), ::GetStockObject(BLACK_PEN));
  288.       break;
  289.  
  290.     case OBJ_BRUSH:
  291.       curObj = ::SelectObject(GetHDC(), ::GetStockObject(BLACK_BRUSH));
  292.       break;
  293.  
  294.     case OBJ_PAL:
  295.       curObj = ::SelectObject(GetHDC(), ::GetStockObject(DEFAULT_PALETTE));
  296.       break;
  297.  
  298.     case OBJ_FONT:
  299.       curObj = ::SelectObject(GetHDC(), ::GetStockObject(DEVICE_DEFAULT_FONT));
  300.       break;
  301.  
  302.     default:
  303.       curObj = 0;           // In non-diagnostic versions, will return 0
  304.       PRECONDITION(curObj); // In diagnostic version will fail a precondition
  305.   }
  306.   if (curObj)
  307.     ::SelectObject(GetHDC(), curObj);  // Restore the object we poped out
  308.  
  309.   return curObj;
  310. #endif
  311. }
  312.  
  313. //
  314. // Compile this in to get this function for 16bit
  315. //
  316. #if defined(BI_PLAT_WIN16)
  317.  
  318. //
  319. // Use undocumented GDI GetClipRgn(HDC) to get the actual handle, and
  320. // make a copy of it. Note that the Win32 GetClipRgn(HDC) does the copying
  321. // itself, while this win16 version requires us to do it.
  322. //
  323. extern "C" HRGN FAR PASCAL GetClipRgn(HDC);  // GDI.173
  324.  
  325. //
  326. //
  327. //
  328. bool
  329. TDC::GetClipRgn(TRegion& region) const
  330. {
  331.   HRGN hRgn = ::GetClipRgn(GetHDC());
  332.   if (!hRgn)
  333.     return false;
  334.   region = TRegion(hRgn);   // make a copy of the region using assignment
  335.   return true;
  336. }
  337. #endif
  338.  
  339. extern "C" bool FAR PASCAL FastWindowFrame(HDC, LPRECT, int xWidth, int yWidth, long rop);
  340.  
  341.  
  342. //
  343. //
  344. //
  345. void
  346. TDC::OWLFastWindowFrame(TBrush& brush, TRect& r, int xThick, int yThick)
  347. {
  348.   SelectObject(brush);
  349.  
  350. #if !defined(BI_PLAT_WIN32)
  351.   if (!FastWindowFrame(GetHDC(), &r, xThick, yThick, PATCOPY))
  352. #endif
  353.   {
  354.     int  width = r.Width() - xThick;
  355.     int  height = r.Height() - yThick;
  356.  
  357.     PatBlt(r.left,        r.top, xThick, height, PATCOPY);  // left
  358.     PatBlt(r.left+xThick, r.top, width, yThick, PATCOPY);   // top
  359.     PatBlt(r.left, r.top+height, width, yThick, PATCOPY);  // bottom
  360.     PatBlt(r.left+width,  r.top+yThick, xThick, height, PATCOPY);  // right
  361.   }
  362.   RestoreBrush();
  363. }
  364.  
  365. //
  366. //
  367. //
  368. int
  369. TDC::SaveDC() const
  370. {
  371.   return ::SaveDC(GetHDC());
  372. }
  373.  
  374. //
  375. //
  376. //
  377. bool
  378. TDC::RestoreDC(int savedIndex)
  379. {
  380.   return ::RestoreDC(GetHDC(), savedIndex);
  381. }
  382.  
  383. //
  384. //
  385. //
  386. int
  387. TDC::GetDeviceCaps(int index) const
  388. {
  389.   return ::GetDeviceCaps(GetAttributeHDC(), index);
  390. }
  391.  
  392. //
  393. //
  394. //
  395. bool
  396. TDC::ResetDC(DEVMODE far& devMode)
  397. {
  398.   return ::ResetDC(GetHDC(), &devMode) != 0;
  399. }
  400.  
  401. //
  402. //
  403. //
  404. TColor
  405. TDC::SetBkColor(const TColor& color)
  406. {
  407.   if (GetHDC() != GetAttributeHDC())
  408.     ::SetBkColor(GetHDC(), color);
  409.   return ::SetBkColor(GetAttributeHDC(), color);
  410. }
  411.  
  412. //
  413. //
  414. //
  415. TColor
  416. TDC::SetTextColor(const TColor& color)
  417. {
  418.   if (GetHDC() != GetAttributeHDC())
  419.     ::SetTextColor(GetHDC(), color);
  420.   return ::SetTextColor(GetAttributeHDC(), color);
  421. }
  422.  
  423. //
  424. //
  425. //
  426. int
  427. TDC::SetMapMode(int mode)
  428. {
  429.   if (GetHDC() != GetAttributeHDC())
  430.     ::SetMapMode(GetHDC(), mode);
  431.   return ::SetMapMode(GetAttributeHDC(), mode);
  432. }
  433.  
  434. //
  435. //
  436. //
  437. bool
  438. TDC::SetViewportOrg(const TPoint& point, TPoint far* oldOrg)
  439. {
  440.   if (GetHDC() != GetAttributeHDC())
  441.     ::SetViewportOrgEx(GetHDC(), point.x, point.y, oldOrg);
  442.   return ::SetViewportOrgEx(GetAttributeHDC(), point.x, point.y, oldOrg);
  443. }
  444.  
  445. //
  446. //
  447. //
  448. bool
  449. TDC::OffsetViewportOrg(const TPoint& delta, TPoint far* oldOrg)
  450. {
  451.   if (GetHDC() != GetAttributeHDC())
  452.     ::OffsetViewportOrgEx(GetHDC(), delta.x, delta.y, oldOrg);
  453.   return ::OffsetViewportOrgEx(GetAttributeHDC(), delta.x, delta.y, oldOrg);
  454. }
  455.  
  456. //
  457. //
  458. //
  459. bool
  460. TDC::SetViewportExt(const TSize& extent, TSize far* oldExtent)
  461. {
  462.   if (GetHDC() != GetAttributeHDC())
  463.     ::SetViewportExtEx(GetHDC(), extent.cx, extent.cy, oldExtent);
  464.   return ::SetViewportExtEx(GetAttributeHDC(), extent.cx, extent.cy, oldExtent);
  465. }
  466.  
  467. //
  468. //
  469. //
  470. bool
  471. TDC::ScaleViewportExt(int xNum, int xDenom, int yNum, int yDenom,
  472.                       TSize far* oldExtent)
  473. {
  474.   if (GetHDC() != GetAttributeHDC())
  475.     ::ScaleViewportExtEx(GetHDC(), xNum, xDenom, yNum, yDenom, oldExtent);
  476.   return ::ScaleViewportExtEx(GetAttributeHDC(), xNum, xDenom, yNum, yDenom, oldExtent);
  477. }
  478.  
  479. //
  480. //
  481. //
  482. bool
  483. TDC::SetWindowOrg(const TPoint& point, TPoint far* oldOrg)
  484. {
  485.   if (GetHDC() != GetAttributeHDC())
  486.     ::SetWindowOrgEx(GetHDC(), point.x, point.y, oldOrg);
  487.   return ::SetWindowOrgEx(GetAttributeHDC(), point.x, point.y, oldOrg);
  488. }
  489.  
  490. //
  491. //
  492. //
  493. bool
  494. TDC::OffsetWindowOrg(const TPoint& delta, TPoint far* oldOrg)
  495. {
  496.   if (GetHDC() != GetAttributeHDC())
  497.     ::OffsetWindowOrgEx(GetHDC(), delta.x, delta.y, oldOrg);
  498.   return ::OffsetWindowOrgEx(GetAttributeHDC(), delta.x, delta.y, oldOrg);
  499. }
  500.  
  501. //
  502. //
  503. //
  504. bool
  505. TDC::SetWindowExt(const TSize& extent, TSize far* oldExtent)
  506. {
  507.   if (GetHDC() != GetAttributeHDC())
  508.     ::SetWindowExtEx(GetHDC(), extent.cx, extent.cy, oldExtent);
  509.   return ::SetWindowExtEx(GetAttributeHDC(), extent.cx, extent.cy, oldExtent);
  510. }
  511.  
  512. //
  513. //
  514. //
  515. bool
  516. TDC::ScaleWindowExt(int xNum, int xDenom, int yNum, int yDenom, TSize far* oldExtent)
  517. {
  518.   if (GetHDC() != GetAttributeHDC())
  519.     ::ScaleWindowExtEx(GetHDC(), xNum, xDenom, yNum, yDenom, oldExtent);
  520.   return ::ScaleWindowExtEx(GetAttributeHDC(), xNum, xDenom, yNum, yDenom, oldExtent);
  521. }
  522.  
  523. //
  524. //
  525. //
  526. bool
  527. TDC::TextOut(int x, int y, const char far* str, int count)
  528. {
  529.   return ::TextOut(GetHDC(), x, y, str, count>=0 ? count : strlen(str));
  530. }
  531.  
  532. //
  533. //
  534. //
  535. bool
  536. TDC::ExtTextOut(int x, int y, uint16 options, const TRect* rect,
  537.                 const char far* str, int count, const int far* dx)
  538. {
  539.   PRECONDITION(count == 0 || str != 0);
  540.   return ::ExtTextOut(GetHDC(), x, y, options, rect, str,
  541.                       count>=0 ? count : strlen(str), (int far*)dx);
  542.                                                       // API typecast
  543. }
  544.  
  545. //
  546. //
  547. //
  548. bool
  549. TDC::TabbedTextOut(const TPoint& p, const char far* str, int count,
  550.                    int numPositions, const int far* positions,
  551.                    int tabOrigin, TSize& size) {
  552.   size = (uint32)::TabbedTextOut(GetHDC(), p.x, p.y, str,
  553.                                  count>=0 ? count : strlen(str),
  554.                                  numPositions, (int far*)positions,
  555.                                  tabOrigin);
  556.                                                 // API typecast
  557.   return true;
  558. }
  559.  
  560. //
  561. //
  562. //
  563. int
  564. TDC::DrawText(const char far* str, int count, const TRect& rect, uint16 format)
  565. {
  566.   return ::DrawText(GetHDC(), str, count,  // uses -1 to signify autocount
  567.                     (RECT*)&rect, format);
  568.                     // API typecast
  569. }
  570.  
  571. //
  572. //
  573. //
  574. bool
  575. TDC::GrayString(const TBrush& brush, GRAYSTRINGPROC outputFunc,
  576.                 const char far* str, int count, const TRect& rect)
  577. {
  578.   return ::GrayString(GetHDC(), brush, outputFunc, (uint32)str,
  579.                       count>=0 ? count : 0,  // uses 0 to signify autocount
  580.                       rect.left, rect.top, rect.Width(), rect.Height());
  581. }
  582.